package games.ThunderFighter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Created by Administrator on 2016/12/13.
 */
public class Myplane implements ActionListener,KeyListener {

    static int x=150,y=550;
    static JButton myplane;
    static Timer timeup,timedown,timeleft,timeright,attack;
    static int bullet=0;
    GameWindow window;
    Bullet equipment;
    static int keyCount;
    //Enemy enemy;

    Myplane(GameWindow window){
        this.window=window;
        myplane=new JButton("飞机");
        window.panel.add(myplane);
        myplane.addActionListener(this);
        myplane.addKeyListener(this);
        myplane.setBounds(x,y,60,30);

        timeup=new Timer(5,this);                                                   //每5ms运动一次
        timedown=new Timer(5,this);
        timeleft=new Timer(5,this);
        timeright=new Timer(5,this);
        attack=new Timer(300,this);


        equipment=new Bullet(window);
        equipment.getBullet();
    }

    public static void changeBullet(){
        if(bullet<2){
            bullet++;
        }
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==timeup){                  //对应的线程调用对应方法
            if(y>0){
                y=y-1;
                myplane.setBounds(x,y,60,30);
            }
        }
        else if(e.getSource()==timedown){
            if(y<575){
                y=y+1;
                myplane.setBounds(x,y,60,30);
            }
    }
        else if(e.getSource()==timeleft){
            if(x>-15){
                x=x-1;
                myplane.setBounds(x,y,60,30);
            }
        }
        else if(e.getSource()==timeright){
            if(x<340){
                x=x+1;
                myplane.setBounds(x,y,60,30);
            }
        }
        else if(e.getSource()==attack){
            if(bullet==0){
                Bullet.primaryBulletAttack(x,y);
            }
            else if(bullet==1){
                Bullet.middleBulletAttack(x,y);
            }
            else if(bullet==2){
                Bullet.highBulletAttack(x,y);
            }
        }
    }
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_UP){
            timeup.start();
            keyCount++;
        }
        else if(e.getKeyCode()==KeyEvent.VK_DOWN){
            timedown.start();
            keyCount++;
        }
        else if(e.getKeyCode()==KeyEvent.VK_LEFT){
            timeleft.start();
            keyCount++;
        }
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            timeright.start();
            keyCount++;
        }
        //else if(e.getKeyCode()==KeyEvent.VK_X){
            //equipment.primaryBulletAttack(x,y);
            //keyCount++;
        //}
        if(keyCount==1){
            Enemy.getEnemy();
            attack.start();
        }
    }
    public void keyReleased(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_UP){
            timeup.stop();
        }
        else if(e.getKeyCode()==KeyEvent.VK_DOWN){
            timedown.stop();
        }
        else if(e.getKeyCode()==KeyEvent.VK_LEFT){
            timeleft.stop();
        }
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            timeright.stop();
        }
    }
    public void keyTyped(KeyEvent e){}

}
